<template>
    <div>
        <div>
            <div>
                <form>
                    商品编号: &nbsp;
                    <el-input placeholder="请输入内容" v-model="selectPrice.id" :on-icon-click="pchandleIconClick"
                              @keyup.enter.native="searchEnterFun" :plain="true"></el-input>
                    销售单价:&nbsp;
                    <el-input v-model="selectPrice.price" :disabled="true"></el-input>
                    销售数量:&nbsp;
                    <el-input v-model="selectPrice.number" placeholder="请输入内容"></el-input>
                    <el-button type="info" @click="doSubmit()">添加</el-button>
                </form>
            </div>
        </div>
        <el-table
                :data="sale"
                style="width: 100%">
            <el-table-column
                    label="商品名称"
                    prop="commodityname">
            </el-table-column>
            <el-table-column
                    label="单价(￥)"
                    prop="unitprice">
            </el-table-column>
            <el-table-column
                    label="数量"
                    prop="number">
            </el-table-column>
            <el-table-column
                    label="总价(￥)"
                    prop="pricetotal">
            </el-table-column>
            <!--<el-table-column-->
            <!--label="操作">-->
            <!--&lt;!&ndash;<el-checkbox v-model="item.isDelete" size="medium">15</el-checkbox>&ndash;&gt;-->
            <!--<el-checkbox label="选择删除" ></el-checkbox>-->
            <!--</el-table-column>-->
        </el-table>
        <el-button type="info" round style="margin-right:0px" @click="jishuan()">结算订单</el-button>
        <el-button type="info" round style="margin-right:-900px" @click="scdd()">删除订单</el-button>
    </div>
</template>


<script>
    import ElInput from "../../../node_modules/element-ui/packages/input/src/input.vue";
    import ElButton from "../../../node_modules/element-ui/packages/button/src/button.vue";

    export default {
        components: {
            ElButton,
            ElInput
        },
        name: "tj",
        data() {
            var date = new Date();
            var seperator1 = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + seperator1 + month + seperator1 + strDate;
            return {
                nowTime: currentdate,
//                isDelete:false,
                UserName: "",
                UserId: "",
                UserSelect: {id: ''},
                StoreId: "",
                product: {
                    id: '',
                    productname: ''
                },
                sale: [
//                    id:'1',
//                    commodityname:'吃撒从',
//                    userid:'',
//                    commodityid	:"",
//                    unitprice:'555',
//                    number:'5',
//                    pricetotal:'5000',
//                    endtime:'2018-1-1',
//                    salesclerkid:'',
//                    storeid:'',
//                    membernumber:''//会员号
                ],
                selectPrice: {
                    id: '',
                    storeid: '',//分店id
                    price: '',//单价
                    unitprice: '',//订单商品单价
                    commodityid: '',//商品id
                    name: '',//商品名字
                    number: '',//数量
                    membernumber: '',//会员号
                    pricetotal: '',//总价
                    state: '1',//订单状态
                    endtime: '',//结算时间,
                    salesclerkid: '',//售货员id
                    commodityid: '',//商品id
                }
            }
        },
        beforeRouteEnter(to, from, next) {
            next(vm => {
                //在此处vm表示vue组件对象
                vm.loadData();
                vm.getSession();
            });
        },
        methods: {
            loadData: function () {
                this.$message({
                    showClose: true,
                    message: this.StoreId,
                    message: this.StoreId,
                    type: 'warning',
                    duration: 1
                });
            },
//            deleteItem(index) {
//                this.form.splice(index, 1)
//                console.log(index)
//            },
            getSession: function () {

                var vm = this;
                this.axios.post("/getname")
                    .then(function (response) {
                        vm.UserSelect.id = response.data.id;
                        vm.salesclerkid = response.data.id;
                        vm.UserName = response.data.realname;
                        vm.StoreId = response.data.storeid;
                        vm.selectPrice.storeid = response.data.storeid;
                    }).catch((error) => {

                })

//                this.$message.info("格调同城祝您工作开心!" + this.StoreId)
            },
            doSubmit: function () {
                var totalPrice = 0;//临时总价
                if (this.selectPrice.id == "" || this.selectPrice.id == null) {
                    this.$message({
                        showClose: true,
                        message: '请输入商品内容!',
                        type: 'warning'
                    });
                } else {
                    var oPrice = this.selectPrice.price;
                    this.selectPrice.unitprice = oPrice;
                    totalPrice = this.selectPrice.unitprice * this.selectPrice.number;
                    this.selectPrice.pricetotal = totalPrice;
                    console.log('add')
                    alert(totalPrice);

                    this.sale.push({
                        text: this.sale.length,
                        commodityid: this.selectPrice.id,
                        commodityname: this.selectPrice.name,
                        number: this.selectPrice.number,
                        unitprice: this.selectPrice.price,
                        pricetotal: this.selectPrice.pricetotal,
                    })


                }
            },
            searchEnterFun: function (e) {
                var vm = this;
                var keyCode = window.event ? e.keyCode : e.which;

                this.axios.get('Http://localhost:8686/selectComById', {params: {id: this.selectPrice.id}})
                    .then((response) => {
                        vm.selectPrice.price = response.data.saleprice;
                        vm.selectPrice.name = response.data.name;
                        if (this.selectPrice.id == "" || this.selectPrice.id == null) {
                            this.$message({
                                showClose: true,
                                message: '请输入商品编号!',
                                type: 'warning'
                            });
                        } else if (response.data.price == "" || response.data.price == null) {
                            this.$message({
                                showClose: true,
                                message: '没有该商品,请核对商品编号!',
                                type: 'error'
                            });

                        }

                    })
            },
            scdd:function () {
                this.sale.splice(0,this.sale.length);//清空数组
                path: '/tc_shopmall_cx'
            },
            jishuan: function () {
//                var totalPrice = 0;//临时总价
//                var oPrice = this.selectPrice.price;
//                this.selectPrice.unitprice = oPrice;
//                totalPrice = this.selectPrice.unitprice * this.selectPrice.number;
//                this.selectPrice.pricetotal = totalPrice;
                var totalPrice = 0;
                for (var i = 0; i < this.sale.length; i++) {
                    totalPrice += this.sale[i].pricetotal;
                }
                this.selectPrice.pricetotal = totalPrice;
                alert(this.selectPrice.pricetotal);
                if (this.selectPrice.id != "" || this.selectPrice.id != null) {
                    let data = {
                        "storeid": this.StoreId,//分店ID
                        "pricetotal": this.selectPrice.pricetotal,//订单总价
                        "state": 0,//状态
                        "endtime": this.nowTime,
                        //"userid":"线下用户",
                        "membernumber": 0,
                        "paytype": '',
                        "salesclerkid": this.salesclerkid,
                        "list": this.sale,
                    }
                    this.axios.post('Http://localhost:8686/insertOrder', data)
                        .then((response) => {
                            this.$message.success(response.data)
                        }).catch((error) => {
                            this.$message.success("前往结算!")
                        })
                    this.$router.push({
                        path: '/tc_shopmall_kc',
                        query: {...this.$route.query, pricetotal: this.selectPrice.pricetotal}
                    })

                }
//                this.$router.push({path: '/tc_shopmall_kc'},this.selectPrice.pricetotal);

            },


        }

    }
</script>

<style scoped>
    .el-input {
        width: 180px;
    }

    .demo-table-expand {
        font-size: 0;
    }

    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }

    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
</style>
